package org.lee;

import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import com.alibaba.druid.support.http.StatViewServlet;
import com.joinway.framework.admin.core.servlet.UEditorServlet;

@SpringBootApplication
@ImportResource("classpath:app-context.xml")
public class Application 
//implements WebApplicationInitializer 
{

//	@Override
//	public void onStartup(ServletContext servletContext) throws ServletException {
//		UEditorServlet servlet = new UEditorServlet();
//		servletContext.addServlet("ueditorServlet", servlet);
//	}

	@Bean public ServletRegistrationBean ueditorServletRegistration(){
		return new ServletRegistrationBean(new UEditorServlet(), "*.do");
	}
	
	@Bean public ServletRegistrationBean statViewServletRegistration(){
		return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
	}
	
	@Bean public FilterRegistrationBean filterRegistrationBean () {
		FilterRegistrationBean bean = new FilterRegistrationBean();
		
		bean.setFilter(new CharacterEncodingFilter());
		bean.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
		bean.addUrlPatterns("/*");
		bean.addInitParameter("encoding", "UTF-8");
		bean.addInitParameter("forceEncoding", "true");
		
		return bean;
	}
	
//	@Bean public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
//	    ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
//	    registration.addUrlMappings("*.json");
//	    registration.setOrder(2);
//	    return registration;
//	}
	
	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(Application.class);
//        ApplicationContext ctx = SpringApplication.run(Application.class, args);
		springApplication.addListeners(new ApplicationPidFileWriter(System.getProperty("user.home") + "/spring-boot.pid"));
		springApplication.run(args);
		
        System.out.println(System.getProperty("user.home"));
        System.out.println("Let's inspect the beans provided by Spring Boot:");
    }

}
